package com.example.lycdays.ui.calculation;

import android.app.DatePickerDialog;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lycdays.R;
import com.example.lycdays.ui.add.AddActivity;
import com.example.lycdays.util.TimeTool;

import java.sql.Time;
import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class CalculationFragment extends Fragment {

    // 状态码，检测用户是否输入事件名、事件日期
    private static final int NO_EVENT_NAME = 1001;
    private static final int NO_EVENT_DATE = 1002;
    private static final int OTHER = 1000;

    //状态码，判断用户点击了哪个datePicker
    private static final int DATE_PICKER_1 = 100;
    private static final int DATE_PICKER_2 = 101;
    private int type = 0;


    private int mYear;
    private int mMonth;
    private int mDay;
    private String eventDate, eventDate1, eventDate2;


    private View datePicker1, datePicker2;
    private Button button;
    TextView textView, textViewDate1, textViewDate2;


    public CalculationFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_calculation, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        textView = getView().findViewById(R.id.textViewResult);
        textView.setVisibility(View.INVISIBLE);

        textViewDate1 = getView().findViewById(R.id.textViewDate1);
        textViewDate2 = getView().findViewById(R.id.textViewDate2);
        textViewDate1.setVisibility(View.INVISIBLE);
        textViewDate2.setVisibility(View.INVISIBLE);

        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        datePicker1 = getView().findViewById(R.id.datePicker);
        datePicker2 = getView().findViewById(R.id.datePicker2);

        datePicker1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = DATE_PICKER_1;
                new DatePickerDialog(getContext(), onDateSetListener, mYear, mMonth, mDay).show();
            }
        });

        datePicker2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = DATE_PICKER_2;
                new DatePickerDialog(getContext(), onDateSetListener, mYear, mMonth, mDay).show();
            }

        });

        button = getView().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDate1 == null || eventDate2 == null) {
                    Toast.makeText(getContext(), "请先选择日期", Toast.LENGTH_SHORT).show();
                } else {
                    int result = TimeTool.caculateDate(eventDate1, eventDate2);
                    result = result > 0 ? result : -result;
                    textView.setText("相隔" + result + "天");
                    textView.setVisibility(View.VISIBLE);

                }
            }
        });



    }

    // 日期选择器的监听
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + mDay;
                }

            } else {
                if (mDay < 10) {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + mDay;
                }

            }

            if (type == DATE_PICKER_1) {
                textViewDate1.setText(days);
                textViewDate1.setVisibility(View.VISIBLE);
                eventDate1 = days;
            } else if (type == DATE_PICKER_2) {
                textViewDate2.setText(days);
                textViewDate2.setVisibility(View.VISIBLE);
                eventDate2 = days;
            }
        }
    };

}
